import React from 'react';
import _ from 'lodash';
import * as authService from '../services/authService';


export default function (Cc) {
  class EnsureLoggedInContainer extends React.Component {

    constructor(props) {
      super(props);
      this.state = {
        isCheck: null,
        message: '',
      };
    }

    componentWillMount() {
      const shopId = this.props.location.query.shopId;
      if (!shopId) {
        this.setState({
          message: '没有shopId',
        });
        return;
      }
      authService.checkShop(shopId).then((data) => {
        if (_.get(data, 'data.success')) {
          this.setState({
            isCheck: true,
          });
        } else {
          this.setState({
            isCheck: false,
            message: '连锁店已被锁定！',
          });
        }
      }).catch(() => {
        this.setState({
          isCheck: false,
          message: '服务异常,请稍后再试',
        });
      });
    }

    render() {
      if (this.state.isCheck) {
        return <Cc {...this.props} />;
      }
      return <div>{this.state.message}</div>;
    }
  }

  return EnsureLoggedInContainer;
}
